import { Body, Controller, Post } from '@nestjs/common'
import { RegisterService } from './register.service'
import { RegisterDto } from './dto/register.dto'
import { User } from '../../../database/entities/user.entity'
import { Public } from 'src/modules/internal-modules/auth/decorators/public.decorator'

@Public()
@Controller('register')
export class RegisterController {
  constructor(private readonly registerService: RegisterService) {}

  @Post('send-code')
  async sendVerificationCode(
    @Body('email') email: string
  ): Promise<{ message: string }> {
    await this.registerService.sendVerificationCode(email)
    return { message: '验证码已发送到邮箱，有效期为5分钟' }
  }

  @Post()
  async register(
    @Body() registerDto: RegisterDto
  ): Promise<{ message: string; user: User }> {
    const user = await this.registerService.verifyAndRegister(registerDto)

    const { password, ...userInfo } = user

    return {
      message: '注册成功',
      user: userInfo as User
    }
  }
}
